package com.hxx.two.jdbc.druida;

import com.hxx.two.jdbc.domain.TUser;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

public class UserServiceImpl implements UserService {

    //创建数据库操作的成员
    private PreparedStatement ps;
    //连接池的连接
    private DbPoolConnection poolConnection = DbPoolConnection.getInstance();

    @Override
    public boolean login(String username, String pwd) {
        String sql = "select count(*) from t_user where username=? and pwd=?";
        //预编译sql
        try {
            ps = poolConnection.getConnection().prepareStatement(sql);
            //设置参数:替换占位符
            ps.setString(1,username);
            ps.setString(2,pwd);
            //执行查询
            ResultSet rs = ps.executeQuery();
            rs.next();//移动到第一行数据

            if (rs.getInt(1)>0) {
                return true;
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return false;
    }

    @Override
    public void save(TUser user) {
        String sql = "insert into t_user(username,sex,pwd) values(?,?,?)";
        //预编译sql
        try {
            ps = poolConnection.getConnection().prepareStatement(sql);
            //设置参数:替换占位符
            ps.setString(1,user.getUsername());
            ps.setString(2,user.getSex());
            ps.setString(3,user.getPwd());
            //执行更新
            ps.execute();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }

    }

    @Override
    public void update(TUser user) {

    }

    @Override
    public void delete(Integer id) {

    }

    @Override
    public TUser findById(Integer id) {
        return null;
    }

    @Override
    public List<TUser> findAll() {
        return null;
    }
}
